<template>
	<view class="main" style="background-color: #e4ecf7;min-height: 100vh;">
		<!-- 已登陆部分 -->
		<view class="user_header_box" v-show="isLogin">
			<view class="avatar_box">
				 <cloud-image :src="avatar_file.url" mode="aspectFill"></cloud-image>
			</view>
			<view class="user_info">
				<view class="name">
				{{ userInfo.username || userInfo.mobile || '未登陆'}}
				</view>
				<view class="uid">
					ID：{{ userInfo._id || '点击登陆账号'}}
				</view>
			</view>
		</view>
		<!-- 未登陆部分 -->
		<view class="user_header_box1" @tap="handleLogin" v-show="!isLogin">
			<view class="avatar_box">
				<image class="img" src="/static/tourist.jpg" mode=""></image>
			</view>
			<view class="user_info">
				<view class="name">
					未登陆
				</view>
				<view class="uid">
					点击登陆账号
				</view>
			</view>
		</view>

		<view class="user_amount_box" >
			<!-- 已登陆部分 -->
			<view class="user_level" v-show="isLogin">
				<image class="icon" src=""></image>
				<view class="level_desc_box" >
					<view class="title">
						Aminos
					</view>
					<view class="desc">
						PetStore
					</view>
				</view>
				<image class="right_icon" src="" mode=""></image>
			</view>
			<!-- 未登录 -->
			<view class="user_level" v-show="!isLogin" @tap="gotoregister">
				<image class="icon" src=""></image>
				<view class="level_desc_box" >
					<view class="title">
						点击去注册
					</view>
					<view class="desc">
						无账号
					</view>
				</view>
				<image class="right_icon" src="" mode=""></image>
			</view>
			
			<!-- 登陆卡片 -->
			<view class="amount_box">
				<view class="amount">
					阿弥诺斯<text style="font-size: 26rpx;">宠物未来</text>
				</view>
				<view class="now_with" @tap="handleLogin" v-show="!isLogin">
					立即登陆
				</view>
				<view class="now_with" @tap="cancelLogin" v-show="isLogin">
					退出登陆
				</view>
			</view>
			
                <view class="divLine"></view>
			<!-- 个人信息卡片 -->
			<view class="amount_box" @tap="gotoPersonal">
				<image class="avatar" src="/static/icons/myInfor.png" mode=""></image>
				<view class="amount">
					个人信息
				</view>
			</view>
			<view class="divLine"></view>
			<!-- 收货地址卡片 -->
			<view class="amount_box" @tap="gotoAddress">
				<image class="avatar" src="/static/icons/address.png" mode=""></image>
				<view class="amount">
					收货地址
				</view>
			</view>
		</view>
		
		
		   
		<!-- 产品列表 -->
		<view class="user_tab_box">
			<view class="tab_title">
				我的订单
			</view>
			<view class="tab_list_box">
				<view class="list_item">
					<image class="icon" src="/static/icons/waitPay.png" mode=""  @tap="gotoMyOrder(1)"></image>
					<view class="text">待付款</view>
				</view>
				<view class="list_item">
					<image class="icon" src="/static/icons/waitDel.png" mode=""  @tap="gotoMyOrder(2)"></image>
					<view class="text">待发货</view>
				</view>
				<view class="list_item">
					<image class="icon" src="/static/icons/waitRec.png" mode=""  @tap="gotoMyOrder(3)"></image>
					<view class="text">待收货</view>
				</view>
				<view class="list_item">
					<image class="icon" src="/static/icons/waitComment.png" mode="" @tap="gotoMyOrder(5)"></image>
					<view class="text">待评价</view>
				</view>
				<view class="list_item">
					<image class="icon" src="/static/icons/afterSale.png" mode=""  @tap="gotoMyOrder(4)"></image>
					<view class="text">售后</view>
				</view>
				<view class="list_item">
					<image class="icon" src="/static/icons/all.png" mode=""  @tap="gotoMyOrder(0)"></image>
					<view class="text">全部</view>
				</view>
				<view class="list_item">
					<image class="icon" src="/static/icons/aboutMine.png" mode="" ></image>
					<view class="text">关于我们</view>
				</view>
				<view class="list_item">
					<image class="icon" src="/static/icons/setting.png" mode=""></image>
					<view class="text">我的设置</view>
				</view>
			</view>
		</view>

		<!-- 猜你喜欢 -->
		<view class="wrap">
			<u-button>猜你喜欢</u-button>
			<u-waterfall v-model="flowList" ref="uWaterfall">
				<template v-slot:left="{leftList}">
					<view class="demo-warter" v-for="(item, index) in leftList" :key="index" @tap="gotoPro(item.productId,item.productByStore)">
						<!-- 警告：微信小程序中需要hx2.8.11版本才支持在template中结合其他组件，比如下方的lazy-load组件 -->
						<u-lazy-load threshold="-450" border-radius="10" :image="item.productImgUrl"
							:index="index"></u-lazy-load>
						<view class="demo-title">
							{{item.productName}}
						</view>
						<view class="demo-price">
							{{item.productPrice}}元
						</view>
						<view class="demo-tag">
							<view class="demo-tag-owner">
								自营
							</view>
							<view class="demo-tag-text">
								放心购
							</view>
						</view>
						<view class="demo-shop">
							{{item.productByStore}}
						</view>
					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="demo-warter" v-for="(item, index) in rightList" :key="index" @tap="gotoPro(item.productId,item.productByStore)">
						<u-lazy-load threshold="-450" border-radius="10" :image="item.productImgUrl"
							:index="index"></u-lazy-load>
						<view class="demo-title">
							{{item.productName}}
						</view>
						<view class="demo-price">
							{{item.productPrice}}元
						</view>
						<view class="demo-tag">
							<view class="demo-tag-owner">
								自营
							</view>
							<view class="demo-tag-text">
								放心购
							</view>
						</view>
						<view class="demo-shop">
							{{item.productByStore}}
						</view>
					</view>
				</template>
			</u-waterfall>
			<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="addRandomData"></u-loadmore>
		</view>
	</view>
</template>

<script>
    import { store } from '@/uni_modules/uni-id-pages/common/store.js'
		import { get, post, removeToken } from '@/utils/api.js';
	import displayList from '../../components/displayList.vue'
	export default {
		computed: {
		  avatar_file() {
			return store.userInfo.avatar_file || {};
		  }
		},
		data() {
			return {
				userInfo:{},
				isLogin:false,
				loadStatus: 'loadmore',
				flowList: [],
				list: [{}],
				currentRoute:'', //当前页面路径
			}
		},
		onLoad() {
			this.onload();
			this.addRandomData();
		},
		onReachBottom() {
			this.loadStatus = 'loading';
			// 模拟数据加载
			setTimeout(() => {
				this.addRandomData();
				this.loadStatus = 'loadmore';
			}, 1)
		},
		created() {
			var pages = getCurrentPages() // 获取栈实例
			 let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
			this.currentRoute = currentRoute
		},
		onShow() {
         //判断是否登陆
		 const token = uni.getStorageSync('uni_id_token');
		 if (token) {
		   // token存在，可以进一步获取或更新用户信息
		   this.userInfo = store.userInfo
		   this.isLogin = true
		 } else {
			 this.userInfo = {}
           this.isLogin = false
		 }
		},
		
		methods: {
			gotoPro(productId,productByStore) {
				uni.setStorageSync('productId',productId)
				uni.setStorageSync('productByStore',productByStore)
				uni.navigateTo({
					url:'/pages/proDetail/proDetail'
				})
			},
			countShopNum()
			{
				post('/cart/getCartCountByUserName',
				 {
					 cartUser:store.userInfo.username
				 }
				 , {
				  needToken: true, 	
					success: (res) => {
					if (res.statusCode === 200) {
					   uni.setStorageSync('shopNum',res.data.data)			  
						}
					}
				}); 
			},
			addRandomData() {
				for (let i = 0; i < 10; i++) {
					let index = this.$u.random(0, this.list.length - 1);
					// 先转成字符串再转成对象，避免数组对象引用导致数据混乱
					let item = JSON.parse(JSON.stringify(this.list[index]))
					item.id = this.$u.guid();
					this.flowList.push(item);
				}
			},
			onload() {
				uni.request({
					url: 'http://10.22.231.3:8181/product/displayProductList',
					method: 'POST',
					success: (res) => {
						if (res.data.code == 300) {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						} else {
							this.list = res.data;
							this.flowList = res.data
						}
					},
					fail: (err) => {
						console.error('请求失败', err);
					}
				});
			},
			gotoregister(){
				uni.navigateTo({
					url:'/uni_modules/uni-id-pages/pages/register/register'
				})
			},
			setUid() {
				post('/user/updateUserInfo', {
				  userName: store.userInfo.username,
				  uid: store.userInfo._id,
				}, {
				  needToken: true, 
				  success: (res) => {
					if (res.statusCode === 200) {
					  console.log('uid设置成功');
					}
				  }
				});
			},
			cancelLogin() {
				this.setUid()
				//清除本地信息
				removeToken()
				store.logout
                uni.removeStorageSync('uni_id_token');
				uni.showToast({
				  title: '您已退出登陆',
				  icon: 'none',
				  duration: 2500
				});
				uni.switchTab({
					url:'/pages/index/index'
				})
			},
			gotoMyOrder(index) {
				uni.setStorageSync('OrderIndex',index)
				uni.navigateTo({
					url:'/pages/myOrder/myOrder'
				})
			},
            handleLogin() {
           // 跳转到登录页面
            uni.navigateTo({
                 url: '/uni_modules/uni-id-pages/pages/login/login-withpwd'
               });
            },
			gotoPersonal() {
				uni.navigateTo({
				     url: '/uni_modules/uni-id-pages/pages/userinfo/userinfo'
				   });
			},
			gotoAddress() {
				uni.navigateTo({
				     url: '/pages/DelAddress/DelAddress'
				   });
			},
		},
		components:{
			displayList
		}
	}
</script>

<style lang="scss">
	/*分割线样式*/
	.divLine{
	 background: #E0E3DA;
	 width: 100%;
	 height: 5rpx;
	}
	.user_header_box{
		height: 340rpx;
		width: 100%;
		background-image: url(/static/pkq1.jpg);
		background-size: 100% 100%;
		display: flex;
		align-items: center;
		justify-content: left;
		.avatar_box{
			margin-left: 80rpx;
			.img{
				height: 120rpx;
				width: 120rpx;
				border-radius: 50%
			}
		}
		.user_info{
			color: #fff;
			margin-left: 20rpx;
			.name{
				color: #ffffff;
				font-size: 40rpx
			}
			.uid{
				color: #ffffff;
				font-size: 20rpx
			}
		}
	}
	.user_header_box1{
		height: 340rpx;
		width: 100%;
		background-image: url(/static/vipbg.jpg);
		background-size: 100% 100%;
		display: flex;
		align-items: center;
		justify-content: left;
		.avatar_box{
			margin-left: 80rpx;
			.img{
				height: 120rpx;
				width: 120rpx;
				border-radius: 50%
			}
		}
		.user_info{
			color: #fff;
			margin-left: 20rpx;
			.name{
				color: #ffffff;
				font-size: 30px
			}
			.uid{
				color: #ffffff;
			}
		}
	}
	.user_amount_box{
		width: 90%;
		margin: -90rpx auto 0 auto;
		.user_level{
			border-radius: 10rpx 10rpx 0 0;
			background-color: #e2eefe;
			padding: 10rpx 14rpx;
			display: flex;
			align-items: center;
			.icon{
				height: 60rpx;
				width: 60rpx;
			}
			.level_desc_box{
				margin-left: 20rpx;
				.title{
					font-size: 28rpx;
				}
				.desc{
					font-size: 20rpx;
					color: #808080;
				}
			}
			.right_icon{
				height: 26rpx;
				width: 16rpx;
				margin-left: auto;
				padding-right: 10rpx;
			}
		}
		.amount_box{
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: #fff;
			border-radius: 0 0 10rpx 10rpx;
			padding: 40rpx 60rpx;
			.amount{
				font-size: 46rpx;
			}
			.now_with{
				background-color: #ffa415;
				color: #fff;
				padding: 8rpx 30rpx;
				border-radius: 5rpx;
                
			}
		}
	}
	.user_tab_box{
		background-color: #fff;
		width: 90%;
		margin: 20rpx auto;
		border-radius: 10rpx;
		padding: 10rpx 20rpx;
		padding-bottom: 20rpx;
		box-sizing: border-box;
		.tab_title{
			font-size: 30rpx;
		}
		.tab_list_box{
			margin-top: 20rpx;
			display: flex;
			flex-wrap: wrap;
			.list_item{
				width: 25%;
				text-align: center;
				margin: 16rpx 0;
				.icon{
					height: 80rpx;
					width: 80rpx;
				}
				.text{
					color: #666666;
					margin-top: -10rpx;
					font-size: 26rpx;
				}
			}
		}
	}
	.loan_box{
		background-color: #fff;
		width: 90%;
		margin: 20rpx auto;
		border-radius: 10rpx;
		padding: 10rpx 20rpx;
		padding-bottom: 20rpx;
		box-sizing: border-box;
		.loan_title_box{
			display: flex;
			align-items: center;
			padding-top: 6rpx;
			.title{
				font-size: 30rpx;
			}
			.link{
				margin-left: auto;
				color: #797979;
				font-size: 20rpx;
			}
		}
		.loan_list{
			margin-top: 30rpx;
			.list_item{
				padding: 0 10rpx;
				border-bottom: 1px solid #e9e9e9;
				margin-top: 20rpx;
				.item_head{
					display: flex;
					align-items: center;
					.pic{
						height: 100rpx;
						width: 100rpx;
					}
					.loan_info{
						margin-left: 20rpx;
						.name{
							color: #797979;
							font-weight: 600;
						}
						.date{
							color: #797979;
							margin-top: 6rpx;
						}
					}
					.join_btn{
						background-color: #2979ff;
						color: #fff;
						margin-left: auto;
						margin-right: 20rpx;
						padding: 6rpx 30rpx;
						border-radius: 30rpx;
						font-size: 28rpx;
					}
				}
				.item_bottom{
					display: flex;
					margin-top: 20rpx;
					padding-bottom: 14rpx;
					width: 100%;
					.bottom_item{
						flex: 1;
						display: flex;
						flex-direction: column;
						align-items: flex-start;
						justify-content: center;
						font-size: 26rpx;
						.names{
							color: #797979;
						}
						.amount{
							color: #ff0548;
							margin-top: 6rpx;
							font-weight: 600;
						}
					}
				}
			}
		}
	}
	.foot_line{
		display: flex;
		align-items: center;
		justify-content: center;
		padding-bottom: 20rpx;
		color: #5f5f5f;
		.line{
			width: 200rpx;
			border-bottom: 1px solid #5f5f5f;
		}
	}
	.avatar{
		height: 50rpx;
		width: 50rpx;
		border-radius: 50%
	}
	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}
	
	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}
	
	.demo-image {
		width: 100%;
		border-radius: 4px;
	}
	
	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
	}
	
	.demo-tag {
		display: flex;
		margin-top: 5px;
	}
	
	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}
	
	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}
	
	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}
	
	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}
</style>
